//
//  LCBaseDatabase+DBSelect.m
//  LCFMDB
//
//  Created by jiukuyinyue on 2017/10/31.
//  Copyright © 2017年 LC. All rights reserved.
//

#import "LCBaseDatabase+DBSelect.h"

@implementation LCBaseDatabase (DBSelect)

- (void)selectEssayAtTableId:(NSString *)Id
                 limitCount:(int)limitCountCount
                offsetCount:(int)offsetCount
                       type:(int)type
                  succBlock:(void(^)(NSArray *array))succBlock
                  failBlock:(void(^)(void))failBlock {
    
    NSString *queryString = nil;
    if (type == SQL_IF_TYPE_FIRST) {
        queryString = [NSString stringWithFormat:@"SELECT DISTINCT title,id,cid,src,timestamp,url,image30,image31,image32 FROM EssayTable WHERE cid = ? order by timestamp desc limit %d",limitCountCount];//sendTime desc
    }
    
    [self.db inDatabase:^(FMDatabase * _Nonnull db) {
        FMResultSet *resultSet = [db executeQuery:queryString,Id];
        
        NSMutableArray *mtbArray = [[NSMutableArray alloc] init];

        while ([resultSet next]) {
            NSDictionary *dict = [NSDictionary dictionary];
            NSString *cid = [resultSet stringForColumn:@"cid"];
            if([cid isEqualToString:@""]) {//如果栏目cid为空
                failBlock();
            }
            dict = @{
                     @"id":[resultSet stringForColumn:@"id"],//文章id
                     @"cid":cid,//文章栏目cid
                     @"title":[resultSet stringForColumn:@"title"],
                     @"src":[resultSet stringForColumn:@"src"],
                     @"timestamp":[resultSet stringForColumn:@"timestamp"],
                     @"url":[resultSet stringForColumn:@"url"],
                     @"image30":[resultSet stringForColumn:@"image30"],
                     @"image31":[resultSet stringForColumn:@"image31"],
                     @"image32":[resultSet stringForColumn:@"image32"]
                     };
            [mtbArray addObject:dict];
        }
        
        if (mtbArray.count != 0) {
            succBlock(mtbArray.copy);
        }else{
            failBlock();
        }
    }];
}

@end
